package com.example.cameraapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

import androidx.constraintlayout.widget.ConstraintLayout;


import com.example.cameraapp.R;

import java.util.ArrayList;
import java.util.List;

public class FaceView extends View {

    private Paint mPaint;
    private int strokeWidth = 5;
    private List<RectF> faces;

    public FaceView(Context context) {
        super(context);
        //构建参数
        initParam();
    }

    private void initParam() {
        faces = new ArrayList<>();
        mPaint = new Paint();
        mPaint.setColor(FaceView.this.getResources().getColor(R.color.white));
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);
        mPaint.setColor(FaceView.this.getResources().getColor(R.color.white));
        mPaint.setAlpha(100);
        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(0, ConstraintLayout.LayoutParams.MATCH_PARENT);
        params.startToStart = R.id.constraint_preview;
        params.topToTop = R.id.constraint_preview;
        this.setLayoutParams(params);
        this.setId(R.id.face_screen);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (RectF face : faces) {
            canvas.drawRect(face.left, face.top, face.right, face.bottom, mPaint);
        }
    }

    public void addFaces(List<RectF> faces) {
        this.faces = faces;
        invalidate();
    }
}
